package org.aphelie.torg.ruler.internal.ui;

import org.aphelie.torg.ruler.internal.ui.editor.GenericSourcePage;
import org.aphelie.torg.ruler.internal.ui.editor.TGRSourcePage;
import org.aphelie.torg.ruler.internal.ui.editor.character.context.InputContext;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.IFormPage;

public abstract class MultiSourceEditor extends TGRFormEditor {

	protected void addSourcePage(String contextId) {
		InputContext context = fInputContextManager.findContext(contextId);
		if (context == null)
			return;
		TGRSourcePage sourcePage;
		// Don't duplicate
		if (findPage(contextId) != null)
			return;
		sourcePage = createSourcePage(this, contextId, context.getInput().getName(), context.getId());
		sourcePage.setInputContext(context);
		try {
			addPage(sourcePage, context.getInput());
		} catch (PartInitException e) {
			TGRPlugin.logException(e);
		}
	}
	
	protected void removePage(String pageId) {
		IFormPage page = findPage(pageId);
		if (page == null)
			return;
		if (page.isDirty()) {
			// need to ask the user about this
		} else {
			removePage(page.getIndex());
			if (!page.isEditor())
				page.dispose();
		}
	}

	protected TGRSourcePage createSourcePage(TGRFormEditor editor, String title, String name, String contextId) {
		System.err.println(this.getClass() + ":createSourcePage" + editor.getSite() );
		return new GenericSourcePage(editor, title, name);
	}
}
